gh-146636: Add Free-threaded Stable ABI migration guide#148453
gh-146636: Add Free-threaded Stable ABI migration guide#148453clin1234 wants to merge 18 commits into
Conversation
da-woods
left a comment
There was a problem hiding this comment.
There's a lot that isn't right here (or is mostly about freethreading generally rather than about the stable ABI).
| :c:func:`PyDict_SetItem`, or explicitly using macros), it attempts to acquire | ||
| the underlying mutex. | ||
|
|
||
| Using Critical Sections |
There was a problem hiding this comment.
The weakness right now is that I don't think they're part of the stable ABI (but hopefully will be)
There was a problem hiding this comment.
If you were referring to https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem, it's part of the Stable ABI: https://docs.python.org/3/c-api/dict.html#c.PyDict_SetItem
There was a problem hiding this comment.
No - I was referring to critical sections. Which I really hope are added before 3.15 comes out but I don't think currently are. Sorry if that was ambiguous.
|
I think the way most people will want to proceed is:
But steps 1 and 2 are the main ones and are almost not covered here. |
|
Based on WIP PR: #148013 |
|
I don't understand why we are having this PR when the reference documentation is not merged. Can we revisit this later? I would have expected the PEP's author to write this. |
|
I left it as a draft because I do not think it is time for it to be reviewed. Please wait for the PEP author first. |
|
Please don't repeat text from free-threading HOWTO. This document should be about migrating from that to the stable ABI; anything that's relevant to free-threading in general should be explained there (and possibly linked from the new document). Also don't repeat information from the reference docs. A migration guide should link to the docs, and perhaps point out how/why the new thing is different. And please see here on how-to guides in general: https://diataxis.fr/how-to-guides/ |
Documentation build overview
101 files changed ·
|
This document provides detailed instructions on how to use the Free Threading Stable ABI in CPython, including guidelines for module initialization, API usage, and thread safety considerations.
Clarified access restrictions on PyObject members and recommended functions for type and reference count manipulation.
Clarified that direct access to PyObject members is prohibited.
Updated documentation to clarify the identification of free-threaded limited API builds in C, including changes to macros and initialization methods.
Updated the documentation to clarify the use of the stable ABI and GIL management in Python extensions, including changes to member access and initialization methods.
Removed sections on API guidelines, critical sections, and thread safety from the documentation.
| The CPython C API exposes the :c:macro:`!Py_LIMITED_API` macro: in the free-threaded stable ABI | ||
| build it's defined to ``1``, and in the regular build it's not defined. |
| - define both :c:macro:`!Py_LIMITED_API` and :c:macro:`!Py_TARGET_ABI3T`, or | ||
| - define only :c:macro:`!Py_LIMITED_API` and: | ||
|
|
||
| - on Windows, define :c:macro:`!Py_GIL_DISABLED`; | ||
| - on other systems, use the headers of free-threaded build of Python. |
There was a problem hiding this comment.
I think the idiomatic way is probably just to define Py_TARGET_ABI3T with the version hex that you want to target.
The other ways (involving defining Py_LIMITED_API and maybe using the free-threading headers) are a bit opaque so it's probably better not to recommend them.
| Accessing any member of ``PyObject`` directly is now prohibited, like the non-GIL | ||
| stable ABI. For instance, prefer ``Py_TYPE()`` and ``Py_SET_TYPE()`` over ``ob_type``, |
There was a problem hiding this comment.
like the non-GIL stable ABI
I think this is the wrong way around and it's "the GIL stable ABI".
I think also that accessing the members is recommended against in the GIL stable ABI and actually impossible in the freethreading stable ABI.
| the GIL disabled; otherwise importing the extension will raise a warning and | ||
| enable the GIL at runtime. | ||
|
|
||
| Multi-phase and single-phase initialization is supported to indicate that an extension module |
There was a problem hiding this comment.
I don't think single-phase initialization is supported
| PySlot_END | ||
| }; | ||
|
|
||
| Furthermore, using ``PyABIInfo_VAR`` and ``Py_mod_abi`` is recommended so that an |
|
|
||
| .. _critical-sections: | ||
|
|
||
| Replacements: |
There was a problem hiding this comment.
At least in the C and C++ world the macros are available and encouraged. So I'm not sure "replacements" makes sense.
| |:c:macro:`Py_BEGIN_CRITICAL_SECTION_MUTEX` |``PyCriticalSection_BeginMutex`` | | ||
| |:c:macro:`Py_END_CRITICAL_SECTION` |``PyCriticalSection_End`` | | ||
| +-------------------------------------------+---------------------------------------+ | ||
| |:c:macro:`Py_BEGIN_CRITICAL_SECTION2_MUTEX`|``PyCriticalSection2_BeginMutex`` | | ||
| |:c:macro:`Py_END_CRITICAL_SECTION2` |``PyCriticalSection2_End`` | | ||
| +-------------------------------------------+---------------------------------------+ |
There was a problem hiding this comment.
I don't think the mutex versions are exposed at all
| ``Py_REFCNT``, ``Py_IncRef()`` and ``Py_DecRef()`` over ``ob_refcnt``, etc. | ||
|
|
||
| Similarly, members of ``PyVarObject`` are not visible. If you need any object of such type | ||
| to be passed as a ``PyObject`` parameter to any API function, cast it directly as ``PyObject``. |
There was a problem hiding this comment.
The big consequence here is not that you can't access the members. It's that you can't define structures like
struct S {
PyObject_HEAD
// your struct goes here
};
and thus have to omit the PyObject_HEAD and set the object size to -size to indicate opaqueness.
You also can't use PyVarObject at all easily right now.
📚 Documentation preview 📚: https://cpython-previews--148453.org.readthedocs.build/